﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model;
using System.Collections.Generic;
using Cn.Edwon.Web;
using Cn.Cellula.Model.Content;

namespace Cn.Cellula.Website.Admin.Content.Vote {
    public partial class EditItem : PageSupport {
        private String ItemKey = null;
        private String VoteKey = null;
        IVoteBiz biz = BizFactory.Get<IVoteBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            ItemKey = Request.QueryString["id"];
            VoteKey = Request.QueryString["t"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(ItemKey)) {
                    FillForm();
                }
            }
        }
        private void FillForm() {
            try {
                TVoteItemEO eo = biz.Load<TVoteItemEO>(ItemKey);
                this.txt_ItemName.Text = eo.ItemContent;
                this.txt_OrderNumber.Text = eo.OrderNumber.ToString();
                this.txt_Score.Text = eo.Score.ToString();
            } catch { }
        }
        private void InitForm() {
            try {
                TVoteEO veo = biz.Load<TVoteEO>(VoteKey);
                this.lab_VoteName.Text = veo.VoteTitle;
            } catch { }
        }

        private void BindData2EO(ref TVoteItemEO eo) {
            eo.ItemContent = this.txt_ItemName.Text;
            try {
                eo.OrderNumber = Convert.ToInt32(this.txt_OrderNumber.Text);
            } catch {
                eo.OrderNumber = 0;
            }
            try {
                eo.Score = Convert.ToInt32(this.txt_Score.Text);
            } catch {
            }
          
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            if (String.IsNullOrEmpty(ItemKey)) {
                TVoteItemEO eo = new TVoteItemEO();
                BindData2EO(ref eo);
                eo.VoteKey = VoteKey;
                biz.Save<TVoteItemEO>(eo);
                BizFactory.Get<ILogBiz>().LogInsert("投票项", eo.ItemContent, this.Actor, Request);
            } else {
                TVoteItemEO eo = biz.Load<TVoteItemEO>(ItemKey);
                BindData2EO(ref eo);
                biz.Update<TVoteItemEO>(eo);
                BizFactory.Get<ILogBiz>().LogUpdate("投票项", eo.ItemContent, this.Actor, Request);
            }
            Response.Redirect("List.aspx?t=" + VoteKey);
            //SiteInfo.SendSucceed(Page, "TypeList.aspx?t=" + eo.TypeKey);
        }
    }
}
